package com.droidexchange.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import com.droidexchange.R;
import com.droidexchange.main.QFilter;
import com.droidexchange.util.Const;
import com.droidexchange.util.Util;

public class QuestionsFilterActivity extends Activity
{
	private QFilter		qFilter	= null;

	private Spinner		spnType	= null;
	private Spinner		spnSort	= null;
	private RadioButton	optDes	= null;
	private RadioButton	optAsc	= null;
	private TextView txtTags = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.questions_filter);
		qFilter = QFilter.getQFilter();

		spnType = (Spinner) findViewById(R.id.type);
		spnType.setSelection(qFilter.getCurrValue(QFilter.TYPE_LIST));

		spnSort = (Spinner) findViewById(R.id.sortby);
		spnSort.setSelection(qFilter.getCurrValue(QFilter.TYPE_SORT));

		optDes = (RadioButton) findViewById(R.id.opt_des);
		optAsc = (RadioButton) findViewById(R.id.opt_asc);

		optDes.setText(qFilter.getLabelFromValue(QFilter.TYPE_ORDER, 0));
		optAsc.setText(qFilter.getLabelFromValue(QFilter.TYPE_ORDER, 1));

		if (qFilter.isDescending())
			optDes.setChecked(true);
		else
			optAsc.setChecked(true);
		
		txtTags = (TextView) findViewById(R.id.txt_clear_tags);
		txtTags.setText(qFilter.getCurrStringValue(QFilter.TYPE_TAGS));
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK)
			return;

		try
		{
			switch (requestCode)
			{
				case (Const.INTENT_TAGS_ACTIVITY):
					String tags = data.getStringExtra("tags");
					txtTags.setText(tags);
				break;
			}
		}
		catch (Exception e)
		{
		}
	}

	public void clickHandler(View v)
	{
		if (v instanceof Button)
		{
			String btnName = (String) ((Button) v).getText();
			if (btnName.equals(getResources().getString(R.string.filter_cancel)))
			{
				Intent intent = new Intent();
				setResult(RESULT_CANCELED, intent);
				finish();
			}
			else if (((Button) v).getText().equals(getResources().getString(R.string.filter_done)))
			{
				boolean nothingChanged = true;
				// TODO convert &= ! to XOR??
				nothingChanged &= !qFilter.setCurrValue(QFilter.TYPE_LIST, spnType.getSelectedItemPosition());
				nothingChanged &= !qFilter.setCurrValue(QFilter.TYPE_SORT, spnSort.getSelectedItemPosition());
				nothingChanged &= !qFilter.setCurrValue(QFilter.TYPE_ORDER, optDes.isChecked() ? 0 : 1);
				nothingChanged &= !qFilter.setCurrValue(QFilter.TYPE_TAGS, txtTags.getText().toString());

				Intent intent = new Intent();
				// TODO - uncomment this
				// setResult(nothingChanged ? RESULT_CANCELED : RESULT_OK,
				// intent);
				setResult(RESULT_OK, intent);
				finish();
			}
			else if (btnName.equals(getResources().getString(R.string.filter_cleartags)))
			{
				txtTags.setText(null);
			}
			else if (btnName.equals(getResources().getString(R.string.filter_changetags)))
			{
				String tags = (String) txtTags.getText();
				Intent intent = new Intent(this, TagsActivity.class);
				intent.putExtra("tags", tags);
				startActivityForResult(intent, Const.INTENT_TAGS_ACTIVITY);
			}
		}
	}
}
